package com.wlj.service;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wlj.entity.UserInfo;
import com.wlj.mapper.UserMapper;

import com.wlj.util.Constant;
import com.wlj.util.JwtTokenUtil;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;



@Service
public class UserServiceImpl   extends ServiceImpl<UserMapper, UserInfo>      implements  UserService {


    @Autowired
    UserMapper userMapper;
    @Override
    public String login(UserInfo user) {
        UserInfo sysUser = userMapper.selectOne(
                new QueryWrapper<UserInfo>().lambda().eq(UserInfo::getUsername, user.getUsername()));
        String token = JwtTokenUtil.createToken(sysUser.getUsername(), Constant.PRIORITY_FRONT_USER);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("Authorization", JwtTokenUtil.TOKEN_PREFIX + token);
        jsonObject.put("userName", sysUser.getUsername());

        return jsonObject.toString();

    }

    @Override
    public String add(UserInfo user) {

        int insert = userMapper.insert(user);
        return insert +"";
    }

    @Override
    public String findOne(UserInfo user) {
        UserInfo sysUser = userMapper.selectOne(
                new QueryWrapper<UserInfo>().lambda().eq(UserInfo::getUsername, user.getUsername()));
        return sysUser.getUsername();
    }

    @Override
    public String update(UserInfo user) {
        QueryWrapper<UserInfo> wrapper = new QueryWrapper<>();
       wrapper.eq("id", user.getId());
        int update = userMapper.update(user, wrapper);
        return "成功更新"+ update+ "条数据";
    }
}
